package com.itheima.service;

import com.itheima.entity.Animal;
import com.itheima.entity.Cat;
import com.itheima.entity.Dog;
import com.itheima.log.AsyncManager;
import com.itheima.log.LogRunnable;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AnimalService {
    List<Animal> animals = new ArrayList<>();
    Scanner sc = new Scanner(System.in);

    public AnimalService() {
        File file = new File("AnimalManager\\dirs\\animals.txt");
        // 检查文件是否存在
        if (!file.exists()) {
            System.out.println("文件不存在");
            return;
        }

        // 检查文件是否为空
        if (file.length() == 0) {
            System.out.println("文件为空");
            return;
        }


        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            while (true){
                try {
                    Animal animal = (Animal) ois.readObject();
                    animals.add(animal);
                } catch (EOFException e) {
                    break;
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public void show() {
        while (true) {
            System.out.println("=====欢迎进入动物管理系统=====");
            System.out.println("1.添加动物");
            System.out.println("2.删除动物");
            System.out.println("3.修改动物");
            System.out.println("4.查询动物");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择：");
            int choice = sc.nextInt();

            switch (choice) {
                case 1:
                    addAnimal();
                    break;
                case 2:
                    removeAnimal();
                    break;
                case 3:
                    updateAnimal();
                    break;
                case 4:
                    selectAnimal();
                    break;
                case 5:
                    saveAnimal();
                    System.out.println("欢迎下次再来！");
                    System.exit(0);
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }

    public void addAnimal() {
        System.out.println("请选择要添加动物的种类（猫/狗）：");
        String type = sc.next();
        System.out.println("请输入动物姓名：");
        String name = sc.next();
        System.out.println("请输入动物年龄：");
        int age = sc.nextInt();
        System.out.println("请输入动物颜色：");
        String color = sc.next();
        if (type.equals("猫")) {
            Animal animal = new Cat(name, age, color);
            animals.add(animal);
            AsyncManager.me().execute(new LogRunnable("添加动物"));
        } else if (type.equals("狗")) {
            Animal animal = new Dog(name, age, color);
            animals.add(animal);
            AsyncManager.me().execute(new LogRunnable("添加动物"));
        } else {
            System.out.println("暂无该种类动物！");
        }
    }

    public void removeAnimal() {
        System.out.println("请输入要删除的动物名字：");
        String name = sc.next();
        for (Animal animal : animals) {
            if (animal.getName().equals(name)) {
                System.out.println("确认删除吗？（y/n）");
                String choice = sc.next();
                if (!choice.equals("y")) {
                    System.out.println("取消删除！");
                    break;
                }
                animals.remove(animal);
                System.out.println("删除成功！");
                AsyncManager.me().execute(new LogRunnable("删除动物"));
                return;
            }
        }
        System.out.println("没有找到该动物！");
    }

    public void selectAnimal() {
        System.out.println("请选择如何查询动物");
        System.out.println("1.查询全部动物");
        System.out.println("2.根据动物名字查询动物");
        System.out.println("3.根据动物种类查询动物（猫/狗）");
        int choice = sc.nextInt();
        boolean flag = true;
        switch (choice) {
            case 1:
                for (Animal animal : animals) {
                    System.out.print(animal + "，");
                    animal.eat();
                    flag = false;
                }
                break;
            case 2:
                System.out.println("请输入要查询的动物名字：");
                String name = sc.next();
                for (Animal animal : animals) {
                    if (animal.getName().equals(name)) {
                        System.out.print(animal + "，");
                        animal.eat();
                        flag = false;
                    }
                }
                break;
            case 3:
                System.out.println("请输入要查询的动物种类（猫/狗）");
                String type = sc.next();
                for (Animal animal : animals) {
                    if (animal instanceof Cat && type.equals("猫")) {
                        System.out.print(animal + "，");
                        animal.eat();
                        flag = false;
                    } else if (animal instanceof Dog && type.equals("狗")) {
                        System.out.print(animal + "，");
                        animal.eat();
                        flag = false;
                    }
                }
                break;
            default:
                System.out.println("输入错误，请重新输入！");
        }
        if (flag) {
            System.out.println("没有找到动物！");
        }
    }

    public void updateAnimal() {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你要修改动物的名称");
        String animalName = sc.next();
        boolean flag = true;
        for (Animal animal : animals) {
            if (animal.getName().equals(animalName)) {
                System.out.println("输入要修改后动物的名称");
                String newname = sc.next();
                animal.setName(newname);
                System.out.println("输入要修改动物的年纪");
                String newage = sc.next();
                animal.setAge(Integer.parseInt(newage));
                System.out.println("输入修改以后的颜色");
                String newcolor = sc.next();
                animal.setColor(newcolor);
                flag = false;
                AsyncManager.me().execute(new LogRunnable("修改动物"));
                System.out.println("修改成功！");
            }
        }
        if (flag) {
            System.out.println("没有找到该动物！");
        }
    }

    public void saveAnimal() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("AnimalManager\\dirs\\animals.txt"))) {
            for (Animal animal : animals) {
                oos.writeObject(animal);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
